home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…tember: Reference Library / Dev.CD Sep 98 RL2.toast / What's New / • What was new 08⁄98 / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / External Tool Templates / CPlus Tool Template / StandardServices.cp < prev    next >
Encoding:
Text File  |  1998-06-04  |  8.6 KB  |  289 lines  |  [TEXT/MPS ]

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //        StandardServices.cp
  4. //    
  5. //        This VUAid Service locks a name so no other actor can "use" it. This is
  6. //        useful for applications that check serial numbers and can only be
  7. //        tested on one target one at a time. The second parameter is true if you
  8. //        want to lock the name, and false if you want to unlock it. The third 
  9. //        parameter is optional. If you supply a string as a key in the 
  10. //        3rd parameter when you lock a name, then you can not unlock it without 
  11. //        supplying the key. This is useful for debugging. CAUTION: If you do not 
  12. //        supply a key during lock, then any script can unlock the name at any time.
  13. //        You are assuming they will be fair and not unlock things you locked.
  14. //
  15. //        The basic rules of operation are:
  16. //            1) The list of names is a global variable that persists until the
  17. //                application is quit and re-launched.
  18. //            2) A specific name can occur only once in the list. 
  19. //            3) If a name exists, it is locked by definition. At the time it is
  20. //                locked, an object containing it is created and added to the list.
  21. //            4) If a name does not exist, it is unlocked by definition. When a 
  22. //                locked name is unlocked, its object is deleted from the list. 
  23. //            5) Trying to lock a locked name fails. ( DELETED unless a key is used and it 
  24. //                matches the key used in the original lock. If no key is used 
  25. //                when trying to lock a locked name, it always fails, even if the 
  26. //                original lock did not set a key. DELETED)
  27. //            6) Trying to unlock a name fails if it is not locked already or if the 
  28. //                key does not match. 
  29. //                
  30. //
  31. //        Parameters:
  32. //            string:        name to lock (up to 63 characters) 
  33. //            Boolean:     true to lock, false to unlock
  34. //            string:        optional key for unlocking (up to 32 characters) 
  35. //
  36. //        Returns:
  37. //            none
  38. //    
  39. //        Revisions:
  40. //            11/23/92    Jonathan Marsh    created (as DebugStrService -- SBR)
  41. //            06/19/94    Stuart Russell    uses new value extraction (see Request.cp)
  42. //            10/16/94    Stuart Russell    change to Pause from DebugStr
  43. //            10/30/94    Stuart Russell    change to Echo from Pause
  44. //
  45. ///////////////////////////////////////////////////////////////////////////////
  46.  
  47. #pragma segment "Services"
  48.  
  49. #ifndef        __StandardServices__
  50. #include        "StandardServices.h"
  51. #endif
  52.  
  53. #ifndef        __Application__
  54. #include        "Application.h"
  55. #endif
  56.  
  57. #ifndef        __THREADS__
  58. #include        "Threads.h"
  59. #endif
  60.  
  61. #include    "StandardServiceErrors.h"
  62.  
  63. #define    kEchoThreadServiceName "EchoThread"
  64. #define    kEchoNoThreadServiceName "EchoNoThread"
  65. #define    kSetSleepTicksServiceName "SetSleepTicks"
  66.  
  67. //—————————————————————————————————————————————————————————————————————————————————————
  68. //                                Global Variables
  69. //—————————————————————————————————————————————————————————————————————————————————————
  70. extern    Application*            gTheApplication;
  71.  
  72. //—————————————————————————————————————————————————————————————————————————————————————
  73. //    EchoThreadService::EchoThreadService    -    constructor.
  74. //—————————————————————————————————————————————————————————————————————————————————————
  75. EchoThreadService::EchoThreadService():ThreadedService( kEchoThreadServiceName )
  76. {
  77. }
  78.  
  79. //—————————————————————————————————————————————————————————————————————————————————————
  80. //    EchoThreadService::~EchoThreadService    -    destructor.
  81. //—————————————————————————————————————————————————————————————————————————————————————
  82. EchoThreadService::~EchoThreadService()
  83. {
  84. }
  85.  
  86. //—————————————————————————————————————————————————————————————————————————————————————
  87. //    EchoThreadService::ProcessRequest    -    implements a service for V.U. to utilize
  88. //—————————————————————————————————————————————————————————————————————————————————————
  89. OSErr
  90. EchoThreadService::ProcessRequest( Request* pReq )
  91. {
  92.     OSErr            tErr;
  93.     short            argc;
  94.     ScriptValue*    pValueToEcho;
  95.     long            pTicksToWait, pBeepEveryN, tReps = 0;
  96.     Boolean            pBeepAtEnd;
  97.     long            ticksWaited;
  98.     long            previousTicks;
  99.     Boolean            done;
  100.  
  101.     previousTicks = TickCount();
  102.     
  103.         // Should have two parameters
  104.     argc = pReq->GetParamCount();
  105.     if (!(argc == 4))            // We have an argument mismatch here
  106.     {
  107.         pReq->SetErrorCode(errParameterCount);
  108.         pReq->SetErrorMessage(errParameterCountStr);
  109.         return errParameterCount;
  110.     }
  111.     
  112.                                             // get pValueToEcho
  113.     tErr = pReq->ExtractValueFromNthParam( 1, &pValueToEcho );
  114.     if (tErr)
  115.         return tErr;
  116.  
  117.                                             // get pTicksToWait
  118.     tErr = pReq->ExtractValueFromNthParam( 2, &pTicksToWait );
  119.     if (tErr)
  120.         return tErr;
  121.  
  122.                                             // get pBeepAtEnd
  123.     tErr = pReq->ExtractValueFromNthParam( 3, &pBeepAtEnd );
  124.     if (tErr)
  125.         return tErr;
  126.     
  127.                                             // get pBeepEveryN
  128.     tErr = pReq->ExtractValueFromNthParam( 4, &pBeepEveryN );
  129.     if (tErr)
  130.         return tErr;
  131.     
  132.     if (pBeepAtEnd)
  133.         SysBeep(1);
  134.  
  135.     done = false;
  136.     
  137.     if( pTicksToWait < 0 )
  138.         SetThreadState( kCurrentThreadID, kStoppedThreadState, kNoThreadID );
  139.     
  140.     ticksWaited = TickCount() - previousTicks;
  141.     if ((ticksWaited >= pTicksToWait) || pReq->HasBeenCanceled())
  142.         done = true;
  143.     while (!done)
  144.     {
  145.         ticksWaited = TickCount() - previousTicks;
  146.         if( (ticksWaited >= pTicksToWait) || CheckForCancel( pReq ) )
  147.             done = true;
  148.  
  149.         if( pBeepEveryN )
  150.         {
  151.             tReps++;
  152.             if( !(tReps % pBeepEveryN) )
  153.                 SysBeep(1);
  154.         }
  155.     }
  156.     
  157.     pReq->SetReturnValue(pValueToEcho);
  158.  
  159.     if (pBeepAtEnd)
  160.         SysBeep(1);
  161.     
  162.     return noErr;
  163. }
  164.  
  165. //—————————————————————————————————————————————————————————————————————————————————————
  166. //    EchoNoThreadService::EchoNoThreadService    -    constructor.
  167. //—————————————————————————————————————————————————————————————————————————————————————
  168. EchoNoThreadService::EchoNoThreadService():Service( kEchoNoThreadServiceName )
  169. {
  170. }
  171.  
  172. //—————————————————————————————————————————————————————————————————————————————————————
  173. //    EchoNoThreadService::~EchoNoThreadService    -    destructor.
  174. //—————————————————————————————————————————————————————————————————————————————————————
  175. EchoNoThreadService::~EchoNoThreadService()
  176. {
  177. }
  178.  
  179. //—————————————————————————————————————————————————————————————————————————————————————
  180. //    EchoNoThreadService::ProcessRequest    -    implements a service for V.U. to utilize
  181. //—————————————————————————————————————————————————————————————————————————————————————
  182. OSErr
  183. EchoNoThreadService::ProcessRequest( Request* pReq )
  184. {
  185.     OSErr            tErr;
  186.     short            argc;
  187.     ScriptValue*    pValueToEcho;
  188.     long            pTicksToWait, pBeepEveryN, tReps = 0;
  189.     Boolean            pBeepAtEnd;
  190.     long            ticksWaited;
  191.     long            previousTicks;
  192.     Boolean            done;
  193.  
  194.     previousTicks = TickCount();
  195.     
  196.         // Should have two parameters
  197.     argc = pReq->GetParamCount();
  198.     if (!(argc == 4))            // We have an argument mismatch here
  199.     {
  200.         pReq->SetErrorCode(errParameterCount);
  201.         pReq->SetErrorMessage(errParameterCountStr);
  202.         return errParameterCount;
  203.     }
  204.     
  205.                                             // get pValueToEcho
  206.     tErr = pReq->ExtractValueFromNthParam( 1, &pValueToEcho );
  207.     if (tErr)
  208.         return tErr;
  209.  
  210.                                             // get pTicksToWait
  211.     tErr = pReq->ExtractValueFromNthParam( 2, &pTicksToWait );
  212.     if (tErr)
  213.         return tErr;
  214.  
  215.                                             // get pBeepAtEnd
  216.     tErr = pReq->ExtractValueFromNthParam( 3, &pBeepAtEnd );
  217.     if (tErr)
  218.         return tErr;
  219.     
  220.                                             // get pBeepEveryN
  221.     tErr = pReq->ExtractValueFromNthParam( 4, &pBeepEveryN );
  222.     if (tErr)
  223.         return tErr;
  224.     
  225.     if (pBeepAtEnd)
  226.         SysBeep(1);
  227.  
  228.     done = false;
  229.     
  230.     ticksWaited = TickCount() - previousTicks;
  231.     if ((ticksWaited >= pTicksToWait) || pReq->HasBeenCanceled())
  232.         done = true;
  233.     while (!done)
  234.     {
  235.         ticksWaited = TickCount() - previousTicks;
  236.         if ((ticksWaited >= pTicksToWait) || CheckForCancel( pReq ))
  237.             done = true;
  238.  
  239.         if( pBeepEveryN )
  240.         {
  241.             tReps++;
  242.             if( !(tReps % pBeepEveryN) )
  243.                 SysBeep(1);
  244.         }
  245.     }
  246.     
  247.     pReq->SetReturnValue(pValueToEcho);
  248.  
  249.     if (pBeepAtEnd)
  250.         SysBeep(1);
  251.     
  252.     return noErr;
  253. }
  254.  
  255.  
  256. //—————————————————————————————————————————————————————————————————————————————————————
  257. //    SetSleepTicksService::SetSleepTicksService    -    constructor.
  258. //—————————————————————————————————————————————————————————————————————————————————————
  259. SetSleepTicksService::SetSleepTicksService():Service( kSetSleepTicksServiceName )
  260. {
  261. }
  262.  
  263. //—————————————————————————————————————————————————————————————————————————————————————
  264. //    SetSleepTicksService::~SetSleepTicksService    -    destructor.
  265. //—————————————————————————————————————————————————————————————————————————————————————
  266. SetSleepTicksService::~SetSleepTicksService()
  267. {
  268. }
  269.  
  270. //—————————————————————————————————————————————————————————————————————————————————————
  271. //    SetSleepTicksService::ProcessRequest    -    implements a service for V.U. to utilize
  272. //—————————————————————————————————————————————————————————————————————————————————————
  273. OSErr
  274. SetSleepTicksService::ProcessRequest( Request* pReq )
  275. {
  276.     OSErr            tErr;
  277.     long            pSleepTicks;
  278.  
  279.     tErr = pReq->ExtractValueFromNthParam( 1, &pSleepTicks );
  280.     if( tErr )
  281.     {
  282.         return tErr;
  283.     }
  284.  
  285.     pReq->SetReturnValue( gTheApplication->SetSleepTicks( pSleepTicks ) );
  286.     return noErr;
  287. }
  288.  
  289.